package com.fxwater.websocket.job;

import cn.hutool.core.collection.CollectionUtil;
import com.fxwater.websocket.util.SpringBeanFinder;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SelectRealData {
    private static JdbcTemplate jdbcTemplate = SpringBeanFinder.findBean(JdbcTemplate.class);
    public static String[] selectRealDataInfo(){

        //查询实时数据表
        String selecteRealDataSql = "select DISTINCT(item_code) item_code from dbo.fx_real_data";
        Set<String> set = new HashSet<String>();
        List<Map<String, Object>> realDataList = jdbcTemplate.queryForList(selecteRealDataSql);
        String[] result = null;
        if(CollectionUtil.isNotEmpty(realDataList)){
            for (int i = 0; i < realDataList.size() ; i++) {
                String str = String.valueOf(realDataList.get(i).get("item_code"));
                set.add(str.split("\\.")[1]);
            }
            if(set.size() > 0){
                result = set.toArray(new String[set.size()]);
            }
        }
        return result;
    }

    public static String[] selectAllItemCode(){

        //查询实时数据表
        String selecteRealDataSql = "select DISTINCT(item_code) item_code from dbo.fx_real_data";
        Set<String> set = new HashSet<String>();
        List<Map<String, Object>> realDataList = jdbcTemplate.queryForList(selecteRealDataSql);
        String[] result = null;
        if(CollectionUtil.isNotEmpty(realDataList)){
            for (int i = 0; i < realDataList.size() ; i++) {
                String str = String.valueOf(realDataList.get(i).get("item_code"));
                set.add(str);
            }
            if(set.size() > 0){
                result = set.toArray(new String[set.size()]);
            }
        }
        return result;
    }

    //初始换redis_cache_info
    public static void initRedisCacheInfo(){
        System.out.println("=============进入初始化redis_cache_info方法=================");
        //删除场景三的数据
        String deleteSql = "DELETE from dbo.fx_redis_cache_info where scene_id = 'THIRD'";
        jdbcTemplate.update(deleteSql);
        //初始化场景三数据
        String[] str = selectRealDataInfo();
        StringBuffer sb = new StringBuffer();
        String remark = null;
        String selectSql = "SELECT DISTINCT(item_name) itemName FROM fx_real_data r LEFT JOIN fx_item i ON r.item_code = i.item_code " +
                "where r.item_code like ?";
        for (int i = 0; i < str.length; i++) {
            List<Map<String, Object>> remarkList = jdbcTemplate.queryForList(selectSql, "%" + str[i]);
            if(remarkList.size() > 0){
                remark = String.valueOf(remarkList.get(0).get("itemName"));
            }
            sb.append("insert into dbo.fx_redis_cache_info(is_valid,is_deleted,create_time,update_time,remark,scene_id,group_id,key_id,redis_prefix) " +
                    "values('1','0',GETDATE(),GETDATE(),'" + remark + "','THIRD','" + str[i] + "','VAL','THIRD_$SCENIID.1_$DATE');");
            sb.append("insert into dbo.fx_redis_cache_info(is_valid,is_deleted,create_time,update_time,remark,scene_id,group_id,key_id,redis_prefix) " +
                    "values('1','0',GETDATE(),GETDATE(),'" + remark + "','THIRD','" + str[i] + "','LAST_UPDATE','THIRD_$SCENIID.1_$DATE');");
        }
        int[] ints = jdbcTemplate.batchUpdate(String.valueOf(sb));
        if(ints.length > 0){
            System.out.println("==================初始化redis_cache_info成功！=====================");
        }else{
            System.out.println("==================初始化redis_cache_info失败！=====================");
        }
    }
}
